package com.ydkj.a37e_mall.Classes.Fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;

/**
 * Created by Administrator on 2017/1/5.
 */

public class SlidingMenu_fg extends Fragment implements View.OnClickListener, OnGetGeoCoderResultListener {

    private View rootView;
    private Activity mActivity;
    private RelativeLayout rly_sliding_gps_adress, rly_sliding_all_classified, rly_sliding_computer_office,
            rly_sliding_home_appliances, rly_sliding_trend_ladies, rly_sliding_trend_mens,
            rly_sliding_nursing_makeup, rly_sliding_drinks, rly_sliding_luxury_gifts, rly_sliding_automobile,
            rly_sliding_articles_taste, rly_sliding_life_travel, rly_sliding_exercise_fitness;
    private TextView tv_sliding_adress;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private GeoCoder mSearch = null; // 搜索模块，也可去掉地图模块独立使用
    private Handler handler;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.sliding_menu, container, false);
        }
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mActivity = getActivity();

//        init();
    }

    private void init() {
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        Log.e("ssss", "经度==sliding》" + sharedPreferencesUtil.getJingdu());
        Log.e("ssss", "纬度==sliding》" + sharedPreferencesUtil.getWeidu());

        mSearch = GeoCoder.newInstance();
        mSearch.setOnGetGeoCodeResultListener(this);


        tv_sliding_adress = (TextView) mActivity.findViewById(R.id.tv_sliding_adress);
        rly_sliding_gps_adress = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_gps_adress);
        rly_sliding_all_classified = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_all_classified);
        rly_sliding_computer_office = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_computer_office);
        rly_sliding_home_appliances = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_home_appliances);
        rly_sliding_trend_ladies = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_trend_ladies);
        rly_sliding_trend_mens = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_trend_mens);
        rly_sliding_nursing_makeup = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_nursing_makeup);
        rly_sliding_drinks = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_drinks);
        rly_sliding_luxury_gifts = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_luxury_gifts);
        rly_sliding_automobile = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_automobile);
        rly_sliding_articles_taste = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_articles_taste);
        rly_sliding_life_travel = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_life_travel);
        rly_sliding_exercise_fitness = (RelativeLayout) mActivity.findViewById(R.id.rly_sliding_exercise_fitness);

        rly_sliding_gps_adress.setOnClickListener(this);
        rly_sliding_all_classified.setOnClickListener(this);
        rly_sliding_computer_office.setOnClickListener(this);
        rly_sliding_home_appliances.setOnClickListener(this);
        rly_sliding_trend_ladies.setOnClickListener(this);
        rly_sliding_trend_mens.setOnClickListener(this);
        rly_sliding_nursing_makeup.setOnClickListener(this);
        rly_sliding_drinks.setOnClickListener(this);
        rly_sliding_luxury_gifts.setOnClickListener(this);
        rly_sliding_automobile.setOnClickListener(this);
        rly_sliding_articles_taste.setOnClickListener(this);
        rly_sliding_life_travel.setOnClickListener(this);
        rly_sliding_exercise_fitness.setOnClickListener(this);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what == 1) {

                    LatLng ptCenter = new LatLng((Double.valueOf(sharedPreferencesUtil.getWeidu())), (Double.valueOf(sharedPreferencesUtil.getJingdu())));
                    mSearch.reverseGeoCode(new ReverseGeoCodeOption()
                            .location(ptCenter));
                }
            }
        };
        new Thread() {
            @Override
            public void run() {
                super.run();
                handler.sendEmptyMessage(1);
            }
        }.start();


    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rly_sliding_gps_adress: //位置定位
//                LatLng ptCenter = new LatLng((Double.valueOf(sharedPreferencesUtil.getWeidu())), (Double.valueOf(sharedPreferencesUtil.getJingdu())));
//                mSearch.reverseGeoCode(new ReverseGeoCodeOption()
//                        .location(ptCenter));
                break;
            case R.id.rly_sliding_all_classified://综合分类
                break;
            case R.id.rly_sliding_computer_office://电脑办公
                break;
            case R.id.rly_sliding_home_appliances://家用电器
                break;
            case R.id.rly_sliding_trend_ladies://潮流女装
                break;
            case R.id.rly_sliding_trend_mens://品牌男装
                break;
            case R.id.rly_sliding_nursing_makeup://护理化妆
                break;
            case R.id.rly_sliding_drinks://酒水饮料
                break;
            case R.id.rly_sliding_luxury_gifts://奢侈礼品
                break;
            case R.id.rly_sliding_automobile://汽车用品
                break;
            case R.id.rly_sliding_articles_taste://计生情趣
                break;
            case R.id.rly_sliding_life_travel://生活旅游
                break;
            case R.id.rly_sliding_exercise_fitness://运动健身
                break;
        }
    }


    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
        Log.e("ssss", "位置==》" + "1111");

    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(mActivity, "抱歉，未能找到结果", Toast.LENGTH_LONG)
                    .show();
            return;
        }

        Log.e("ssss", "位置==》" + result.getAddress());
        String adress = result.getAddress();
        if (adress.equals("")) {
            tv_sliding_adress.setText("");
            return;
        } else {
            String cityname = adress.substring(3, 6);
            tv_sliding_adress.setText(cityname);
            Toast.makeText(mActivity, result.getAddress(),
                    Toast.LENGTH_LONG).show();

        }
    }
}
